import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";

/**
 * client.js - 客户端 model
 * @date: 2018-12-24
 */
import { getResponse, createPagination, addItemsToPagination } from 'utils/utils';
import { queryIdpValue, getPublicKey as _getPublicKey } from 'cpaas-front/lib/services/api';
import { fetchClientList as _fetchClientList, fetchDetail as _fetchDetail, checkClient as _checkClient, createClient as _createClient, updateClient as _updateClient, // deleteClient,
subAccountOrgRoleQueryAll, subAccountOrgRoleCurrent, // saveRoleSet,
deleteRoles as _deleteRoles, roleVisitCurrent as _roleVisitCurrent, deleteVisitRoles as _deleteVisitRoles, saveVisitRoleSet as _saveVisitRoleSet } from "../services/clientService";
export default {
  namespace: 'client',
  state: {
    clientList: [],
    // 列表数据
    pagination: {},
    // 分页器
    detailData: {},
    // 详情数据
    typeList: [],
    // 授权类型
    randomInfoData: [],
    // 需新建客户端时生成的随机数据
    ownedRoleList: [],
    // 拥有的角色
    paginationRole: {},
    publicKey: '',
    // 密码公钥
    visitRoleList: [],
    visitRolePagination: {}
  },
  effects: {
    // 获取授权类型信息
    queryType: /*#__PURE__*/_regeneratorRuntime.mark(function queryType(_, _ref) {
      var call, put, res, typeList;
      return _regeneratorRuntime.wrap(function queryType$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              call = _ref.call, put = _ref.put;
              _context.next = 3;
              return call(queryIdpValue, 'CIAM.GRANT_TYPE');

            case 3:
              res = _context.sent;
              typeList = getResponse(res);
              _context.next = 7;
              return put({
                type: 'updateState',
                payload: {
                  typeList: typeList
                }
              });

            case 7:
            case "end":
              return _context.stop();
          }
        }
      }, queryType);
    }),
    // 查询Client列表数据
    fetchClientList: /*#__PURE__*/_regeneratorRuntime.mark(function fetchClientList(_ref2, _ref3) {
      var payload, call, put, res, list;
      return _regeneratorRuntime.wrap(function fetchClientList$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref2.payload;
              call = _ref3.call, put = _ref3.put;
              _context2.next = 4;
              return call(_fetchClientList, payload);

            case 4:
              res = _context2.sent;
              list = getResponse(res);

              if (!list) {
                _context2.next = 9;
                break;
              }

              _context2.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  clientList: list.content,
                  pagination: createPagination(list)
                }
              });

            case 9:
              return _context2.abrupt("return", list);

            case 10:
            case "end":
              return _context2.stop();
          }
        }
      }, fetchClientList);
    }),
    // 查询详情
    fetchDetail: /*#__PURE__*/_regeneratorRuntime.mark(function fetchDetail(_ref4, _ref5) {
      var payload, call, put, res, list;
      return _regeneratorRuntime.wrap(function fetchDetail$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref4.payload;
              call = _ref5.call, put = _ref5.put;
              _context3.next = 4;
              return call(_fetchDetail, payload);

            case 4:
              res = _context3.sent;
              list = getResponse(res);

              if (!list) {
                _context3.next = 9;
                break;
              }

              _context3.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  detailData: list
                }
              });

            case 9:
              return _context3.abrupt("return", list);

            case 10:
            case "end":
              return _context3.stop();
          }
        }
      }, fetchDetail);
    }),
    // 校验
    checkClient: /*#__PURE__*/_regeneratorRuntime.mark(function checkClient(_ref6, _ref7) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function checkClient$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref6.payload;
              call = _ref7.call;
              _context4.next = 4;
              return call(_checkClient, payload);

            case 4:
              res = _context4.sent;
              return _context4.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context4.stop();
          }
        }
      }, checkClient);
    }),
    // 创建
    createClient: /*#__PURE__*/_regeneratorRuntime.mark(function createClient(_ref8, _ref9) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function createClient$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              payload = _ref8.payload;
              call = _ref9.call;
              _context5.next = 4;
              return call(_createClient, payload);

            case 4:
              res = _context5.sent;
              return _context5.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context5.stop();
          }
        }
      }, createClient);
    }),
    // 更新LDAP
    updateClient: /*#__PURE__*/_regeneratorRuntime.mark(function updateClient(_ref10, _ref11) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function updateClient$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              payload = _ref10.payload;
              call = _ref11.call;
              _context6.next = 4;
              return call(_updateClient, payload);

            case 4:
              res = _context6.sent;
              return _context6.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context6.stop();
          }
        }
      }, updateClient);
    }),
    // 删除
    // *deleteClient({ payload }, { call }) {
    //   const res = yield call(deleteClient, payload);
    //   return getResponse(res);
    // },
    // 当前登录用户所拥有的id
    roleQueryAll: /*#__PURE__*/_regeneratorRuntime.mark(function roleQueryAll(_ref12, _ref13) {
      var payload, call, put, res, createSubRoles;
      return _regeneratorRuntime.wrap(function roleQueryAll$(_context7) {
        while (1) {
          switch (_context7.prev = _context7.next) {
            case 0:
              payload = _ref12.payload;
              call = _ref13.call, put = _ref13.put;
              _context7.next = 4;
              return call(subAccountOrgRoleQueryAll, payload);

            case 4:
              res = _context7.sent;
              createSubRoles = getResponse(res);

              if (!res) {
                _context7.next = 9;
                break;
              }

              _context7.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  createSubRoles: createSubRoles
                }
              });

            case 9:
              return _context7.abrupt("return", createSubRoles);

            case 10:
            case "end":
              return _context7.stop();
          }
        }
      }, roleQueryAll);
    }),
    // 查询当前用户所拥有的角色
    roleCurrent: /*#__PURE__*/_regeneratorRuntime.mark(function roleCurrent(_ref14, _ref15) {
      var payload, call, put, res, list;
      return _regeneratorRuntime.wrap(function roleCurrent$(_context8) {
        while (1) {
          switch (_context8.prev = _context8.next) {
            case 0:
              payload = _ref14.payload;
              call = _ref15.call, put = _ref15.put;
              _context8.next = 4;
              return call(subAccountOrgRoleCurrent, payload);

            case 4:
              res = _context8.sent;
              list = getResponse(res);

              if (!list) {
                _context8.next = 9;
                break;
              }

              _context8.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  ownedRoleList: list.content,
                  // list.content
                  paginationRole: createPagination(list)
                }
              });

            case 9:
              return _context8.abrupt("return", list);

            case 10:
            case "end":
              return _context8.stop();
          }
        }
      }, roleCurrent);
    }),
    // 保存角色
    saveRoleSet: /*#__PURE__*/_regeneratorRuntime.mark(function saveRoleSet(_ref16, _ref17) {
      var payload, put, select, state, ownedRoleList, paginationRole, arr;
      return _regeneratorRuntime.wrap(function saveRoleSet$(_context9) {
        while (1) {
          switch (_context9.prev = _context9.next) {
            case 0:
              payload = _ref16.payload;
              put = _ref17.put, select = _ref17.select;
              _context9.next = 4;
              return select(function (st) {
                return st.client;
              });

            case 4:
              state = _context9.sent;
              ownedRoleList = state.ownedRoleList, paginationRole = state.paginationRole;
              arr = payload.map(function (r) {
                return _objectSpread(_objectSpread({}, r), {}, {
                  _status: 'create'
                });
              }); // const ownedRoleLists = ownedRoleList.concat(arr);

              _context9.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  // ownedRoleList: ownedRoleLists, // list.content
                  paginationRole: addItemsToPagination(payload.length, ownedRoleList.length, paginationRole)
                }
              });

            case 9:
              return _context9.abrupt("return", arr);

            case 10:
            case "end":
              return _context9.stop();
          }
        }
      }, saveRoleSet);
    }),
    // 删除角色
    deleteRoles: /*#__PURE__*/_regeneratorRuntime.mark(function deleteRoles(_ref18, _ref19) {
      var payload, call, memberRoleList, res;
      return _regeneratorRuntime.wrap(function deleteRoles$(_context10) {
        while (1) {
          switch (_context10.prev = _context10.next) {
            case 0:
              payload = _ref18.payload;
              call = _ref19.call;
              memberRoleList = payload.memberRoleList;
              _context10.t0 = getResponse;
              _context10.next = 6;
              return call(_deleteRoles, memberRoleList);

            case 6:
              _context10.t1 = _context10.sent;
              res = (0, _context10.t0)(_context10.t1);
              return _context10.abrupt("return", res);

            case 9:
            case "end":
              return _context10.stop();
          }
        }
      }, deleteRoles);
    }),
    // 查询当前用户可访问的角色
    roleVisitCurrent: /*#__PURE__*/_regeneratorRuntime.mark(function roleVisitCurrent(_ref20, _ref21) {
      var payload, call, put, res, list;
      return _regeneratorRuntime.wrap(function roleVisitCurrent$(_context11) {
        while (1) {
          switch (_context11.prev = _context11.next) {
            case 0:
              payload = _ref20.payload;
              call = _ref21.call, put = _ref21.put;
              _context11.next = 4;
              return call(_roleVisitCurrent, payload);

            case 4:
              res = _context11.sent;
              list = getResponse(res);

              if (!list) {
                _context11.next = 9;
                break;
              }

              _context11.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  visitRoleList: list.content,
                  // list.content
                  visitRolePagination: createPagination(list)
                }
              });

            case 9:
              return _context11.abrupt("return", list);

            case 10:
            case "end":
              return _context11.stop();
          }
        }
      }, roleVisitCurrent);
    }),
    // 保存角色
    saveVisitRoleSet: /*#__PURE__*/_regeneratorRuntime.mark(function saveVisitRoleSet(_ref22, _ref23) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function saveVisitRoleSet$(_context12) {
        while (1) {
          switch (_context12.prev = _context12.next) {
            case 0:
              payload = _ref22.payload;
              call = _ref23.call;
              _context12.next = 4;
              return call(_saveVisitRoleSet, payload);

            case 4:
              res = _context12.sent;
              return _context12.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context12.stop();
          }
        }
      }, saveVisitRoleSet);
    }),
    // 删除角色
    deleteVisitRoles: /*#__PURE__*/_regeneratorRuntime.mark(function deleteVisitRoles(_ref24, _ref25) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function deleteVisitRoles$(_context13) {
        while (1) {
          switch (_context13.prev = _context13.next) {
            case 0:
              payload = _ref24.payload;
              call = _ref25.call;
              _context13.t0 = getResponse;
              _context13.next = 5;
              return call(_deleteVisitRoles, payload);

            case 5:
              _context13.t1 = _context13.sent;
              res = (0, _context13.t0)(_context13.t1);
              return _context13.abrupt("return", res);

            case 8:
            case "end":
              return _context13.stop();
          }
        }
      }, deleteVisitRoles);
    }),
    getPublicKey: /*#__PURE__*/_regeneratorRuntime.mark(function getPublicKey(_, _ref26) {
      var call, put, res;
      return _regeneratorRuntime.wrap(function getPublicKey$(_context14) {
        while (1) {
          switch (_context14.prev = _context14.next) {
            case 0:
              call = _ref26.call, put = _ref26.put;
              _context14.next = 3;
              return call(_getPublicKey);

            case 3:
              res = _context14.sent;

              if (!res) {
                _context14.next = 7;
                break;
              }

              _context14.next = 7;
              return put({
                type: 'updateState',
                payload: {
                  publicKey: res.publicKey
                }
              });

            case 7:
              return _context14.abrupt("return", res);

            case 8:
            case "end":
              return _context14.stop();
          }
        }
      }, getPublicKey);
    })
  },
  reducers: {
    updateState: function updateState(state, action) {
      return _objectSpread(_objectSpread({}, state), action.payload);
    }
  }
};